package itsur.arquitecturas.pipeandfilter;

/**************************************************************************************
 ** Class name: MainFilter
 ** Author: A.J. Lattanze
 ** Date: 12/3/99
 ** Version 1.0
 ***************************************************************************************
 ** Purpose: Assignment 1 for SE 17-655, Architectures for Software Systems.  This
 ** assignment is designed to illustrate a pipe and filter architecture.  For the 
 ** instructions, refer to the assignment write-up.
 **
 ** Abstract:This class contains the main method for assignment 1.  The assignment 1
 ** program consists of 4 files:
 ** 1) MainFilter - Starts the 3 filters (2 CourseFilter) reads each student 
 **    entry from the input file and separates the MSE students from non-MSE students 
 **   	and writes them to the appropriate filter.
 ** 2) CourseFilter - Will determine if a student entry has taken a particular course
 **    that is specified during instantiation.
 ** 3) MergeFilter - Accepts input from up to 5 input sources and writes it to an output
 **    file.
 ** 
 ** Pseudo Code:
 **
 **	start CourseFilter for 15671
 **	start CourseFilter for 21701
 **	start MergeFilter
 **	open input file provided on command line
 **	while not eof (input file)
 **		read line of text
 **		If "MSE" appears on line of text
 **			write line of text to CourseFilter for 15651
 **			flush pipe
 **		else
 **			write line of text to CourseFilter for 21701
 **			flush pipe
 **		end
 **	end while
 **	close pipes and files
 **
 ** Running the program
 **
 ** 	Java MainFilter IputFile OutputFile > ListingFile
 **		
 **		MainFilter - Program name
 **		InputFile - Text file in the format (see comments below)
 **		OutputFile - Text output file with students
 **		ListingFile - Optional file to direct optional debug statements
 **
 ** Modification Log
 **************************************************************************************
 **
 **************************************************************************************/
import java.io.*;

public class MainFilter {
	public static void main(String argv[]) {
		// Lets make sure that input and output files are provided on the
		// command line
		//itsur.java
		//asegurarse de que sean 3 parametros de entrada 
		//parametro 0: archivo de entrada
		//parametro 1: alumnos aceptados
		//parametro 2: alumnos no aceptados

		if (argv.length != 3) {
			System.out
					.println("\n\nIncorrect number of input parameters - correct usage:");
			System.out
					.println("\nJava MainFilter <input file name> <output accepted file name> <output rejected file name>");

		} else {

			// Declarations:
			String lineOfText;
			File inputFile = new File(argv[0]);

			// These are the declarations for the pipes.

			PipedWriter courseFilter1PipeWriter = new PipedWriter();
			PipedWriter courseFilter2PipeWriter = new PipedWriter();
			PipedReader mergeFilter1PipeReader = new PipedReader();
			PipedReader mergeFilter2PipeReader = new PipedReader();
				//itsur.java
			PipedReader mergeRejectFilter1PipeReader = new PipedReader();
			PipedReader mergeRejectFilter2PipeReader = new PipedReader();
			
			// Instantiate the Course Filter Threads
			Thread courseFilter1 = new CourseFilter("12333 or 13444", courseFilter1PipeWriter,
					mergeFilter1PipeReader,mergeRejectFilter1PipeReader);
			Thread courseFilter2 = new CourseFilter("( 12333 or 13444 ) and 13222", courseFilter2PipeWriter,
					mergeFilter2PipeReader,mergeRejectFilter2PipeReader);

			// Instantiate the Merge Filter Threads
			Thread mergeFilter1 = new MergeFilter(argv[1], mergeFilter1PipeReader,
					mergeFilter2PipeReader);
			//itsur.java
			Thread mergeFilter2 = new MergeRejectFilter(argv[2], mergeRejectFilter1PipeReader,
					mergeRejectFilter2PipeReader);
			// Start the threads (these are the filters)

			courseFilter1.start();
			courseFilter2.start();
			mergeFilter1.start();
			mergeFilter2.start();
			
			// Open input file. The input file is a field oriented and
			// space-separated.
			// The fields are as follows:
			// 
			// Social Student's Program Course numbers that the student
			// Security Last Name Acronym has completed with no hyphens
			//
			// Here is an example:
			//
			// 324123123 Franklin GSIA 12323 21323 12123

			// Check to ensure that the file exists

			if (!inputFile.exists()) {
				System.out.print("\nMainFilter:: file ");
				System.out.println(inputFile.getAbsolutePath());
				System.out.println(" does not exist.");

			} else {

				System.out.print("MainFilter:: file "
						+ inputFile.getAbsolutePath());
				System.out.println(" opened.");

				try {
					// Create a buffered reader the file
					BufferedReader inFile = new BufferedReader(
							new InputStreamReader(new FileInputStream(
									(inputFile))));

					// Read each line of text

					boolean done = false;

					while (!done) {
						// Read a line of text from the input file and convert
						// it to upper case

						lineOfText = inFile.readLine();

						if (lineOfText == null) {

							done = true;

						} else {
							lineOfText.toUpperCase();

							System.out.println("MainFilter:: read: "
									+ lineOfText);

							if (lineOfText.indexOf("GSIA") != -1) {

								// This is an MSE student. This text needs to be
								// sent to
								// CourseFilter1 to ensure that they have taken
								// or are registered
								// for the models course

								try {
									// write line of text to the pipe
									courseFilter1PipeWriter.write(lineOfText, 0,
											lineOfText.length());
									courseFilter1PipeWriter.write((int) '\n'); // signals
																			// end
																			// of
																			// line
									courseFilter1PipeWriter.flush();
									System.out.print("MainFilter:: wrote: "
											+ lineOfText);
									System.out.println(" to " + " filter #1");

								} // try

								catch (Exception Error) {
									System.out
											.println("MainFilter::Error writing to CourseFilter1. ");
								} // catch

							} else {

								// This is not an MSE student. This text needs
								// to be sent to
								// CourseFilter2 to ensure that they have taken
								// or are registered
								// for the discrete math course

								try {
									// write line of text to the pipe

									courseFilter2PipeWriter.write(lineOfText, 0,
											lineOfText.length());
									courseFilter2PipeWriter.write((int) '\n'); // signals
																			// end
																			// of
																			// line
									courseFilter2PipeWriter.flush();
									System.out.print("MainFilter:: wrote: "
											+ lineOfText);
									System.out.println(" to " + " filter #2 ");

								} // try

								catch (Exception Error) {
									System.out
											.println("MainFilter::Error writing to CourseFilter2. ");
								} // catch
							} // if
						} // if
					} // while

					// Close the input file

					inFile.close();
					System.out.print("\nMainFilter:: closing ");
					System.out.println(inputFile.getAbsolutePath());

				} // try

				catch (Exception Error) {
					System.out
							.println("MainFilter::Unable to create stream file ");
				} // catch

			} // if

			try {
				courseFilter1PipeWriter.close();
				System.out.println("MainFilter::filter #1 pipe closed ");
				courseFilter2PipeWriter.close();
				System.out.println("MainFilter::filter #2 pipe closed ");

			} // try

			catch (Exception Error) {
				System.out.println("MainFilter::Error closing pipes ");
			} // catch
		} // if
	} // main
} // MainFilter